package com.atguigu.apiservice.service.impl;

import com.atguigu.apiservice.entity.Book;
import com.atguigu.apiservice.mapper.BookMapper;
import com.atguigu.apiservice.service.BookService;
import com.atguigu.servicebase.VO.BookQueryVo;

import com.atguigu.servicebase.VO.FrontVo;
import com.atguigu.servicebase.VO.OneBookVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-26
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {


    /**
     * 分页展示书籍数据带条件
     * @param pageParam
     * @param bookQueryVo
     * @return
     */
    @Override
    public Map<String, Object> pageListVo(Page<Book> pageParam, BookQueryVo bookQueryVo) {
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        String labelId = bookQueryVo.getLableId();//分类Id
        if(!StringUtils.isEmpty(labelId)){
            bookQueryWrapper.eq("label_id",labelId);
        }
        baseMapper.selectPage(pageParam,bookQueryWrapper);
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        List<Book> records = pageParam.getRecords();

        Map<String,Object> maps = new HashMap<>();
        maps.put("items", records);
        maps.put("current", current);
        maps.put("pages", pages);
        maps.put("size", size);
        maps.put("total", total);

        return maps;
    }

    /**
     * 根据Id获取书籍详情
     * @param id
     * @return
     */
    @Override
    public FrontVo getBookInfo(String id) {
        FrontVo bookVoInfo = baseMapper.getBookVoInfo(id);
        return bookVoInfo;
    }

    /**
     * 得到所有一级分类
     * @return
     */
    @Override
    public List<OneBookVo> getAllBookLable() {
        //获得一级分类数据
        QueryWrapper<OneBookVo> wrapper = new QueryWrapper<>();
        wrapper.eq("photo",null);
        List<Book> oneBookVo = baseMapper.getOneBookVo();

        List<OneBookVo> oneBookVoList = new ArrayList<>();
        //封装一级分类
        for (int i = 0; i < oneBookVo.size(); i++) {
           //获得每一个一级分类
            Book oneBook = oneBookVo.get(i);
            OneBookVo newOneBook = new OneBookVo();
            //将oneBook数据放入newOneBook中
            newOneBook.setId(oneBook.getLabelId());
            newOneBook.setName(oneBook.getName());
            oneBookVoList.add(newOneBook);
        }
        return oneBookVoList;
    }

    @Override
    public void infoupload(Book book) {
        book.setCover("https://bookshore-ords.oss-cn-beijing.aliyuncs.com/2020/06/07/5b828849-f57c-47d3-b690-ddcd33939745s29905197.jpg");
        baseMapper.insert(book);
    }


}
